package com.xbai.spark.core.action

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
  * fold(num)(func) 算子
  * 作用：折叠操作，aggregate 的简化操作，seqop 和 combop 一样。
  * @author xbai
  * @Date 2020/12/31
  */
object Spark08_Fold {

  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("fold")
    val sc = new SparkContext(conf)

    val rdd: RDD[Int] = sc.parallelize(1 to 10, 2)
    rdd.glom().collect().foreach(x => println(x.mkString(", ")))
    val result: Int = rdd.fold(0)(_ + _)
    println(result)

    val result2: Int = rdd.fold(5)(_ + _)
    println(result2)
    sc.stop()
  }
}
